//unit: mm
maxsize = 5;
cl__1 = 0.5;
coilcl = 0.2;

cH = 2.5;
cW = 0.4;
cR = 1.05;
cdx1 = 0;
cdx2 = 0;
cdy1 = -1.8;
cdy2 = -1.8;
cdz1 = 2.1;
cdz2 = -2.1;
r1 = cR+cW/2;
r2 = cR-cW/2;


pcenter = newp;
Point(pcenter+1) = {cdx2, cdy2-cH/2, cdz2};

Point(pcenter+2) = {cdx2-r1, cdy2-cH/2, cdz2,coilcl};
Point(pcenter+3) = {cdx2, cdy2-cH/2, cdz2+r1,coilcl};
Point(pcenter+4) = {cdx2+r1, cdy2-cH/2, cdz2,coilcl};
Point(pcenter+5) = {cdx2, cdy2-cH/2, cdz2-r1,coilcl};



c5 = newc;
Circle(c5+1) = {pcenter+2,pcenter+1,pcenter+3};
Circle(c5+2) = {pcenter+3,pcenter+1,pcenter+4};
Circle(c5+3) = {pcenter+4,pcenter+1,pcenter+5};
Circle(c5+4) = {pcenter+5,pcenter+1,pcenter+2};



l5 = newll;
Curve Loop(l5) = {c5+1,c5+2,c5+3,c5+4};
// l6 = newll;
// Curve Loop(l6) = {c5+5,c5+6,c5+7,c5+8};
s3 = news;
Plane Surface(s3) = {l5};

Extrude {0, cH, 0} {Surface{s3}; }


inf = 2;
//create air box around magnets
BoundingBox; // recompute model bounding box
cx = (General.MinX + General.MaxX) / 2;
cy = (General.MinY + General.MaxY) / 2;
cz = (General.MinZ + General.MaxZ) / 2;
lx = 2*inf + General.MaxX - General.MinX;
ly = 2*inf + General.MaxY - General.MinZ;
lz = 2*inf + General.MaxZ - General.MinZ;
p1 = newp; Point (p1) = {cx-lx/2, cy-ly/2, cz-lz/2, maxsize};
p2 = newp; Point (p2) = {cx+lx/2, cy-ly/2, cz-lz/2, maxsize};
l1 = newl; Line(l1) = {p1, p2};
e1[] = Extrude {0, ly, 0} { Line{l1}; };
e2[] = Extrude {0, 0, lz} { Surface{e1[1]}; };
Delete { Volume{e2[1]}; }//delete the box before create new one
ss[] = {e1[1],e2[0],e2[2],e2[3],e2[4],e2[5]};
sl1 = newsl; Surface Loop(sl1) = {ss[]};
vv[] = {sl1};


skin[] = CombinedBoundary{ Volume{1}; };
sl = newsl; Surface Loop(sl) = skin[];
vv[] += sl;
// skin[] = CombinedBoundary{ Volume{2}; };
// sl = newsl; Surface Loop(sl) = skin[];
// vv[] += sl;

v1 = newv; 
Volume(2) = {vv[]};